package com.one.controller;

import com.one.pojo.*;
import com.one.service.*;
import com.one.util.EhcacheCacheProvider;
import com.one.util.JSONResult;
import com.one.util.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 农场管理
 * @Author semstouch
 * @Date 2017/4/11
 **/
@Controller
@ControllerAdvice
public class FarmController extends AbstractJsonpResponseBodyAdvice {
    @Resource
    private OneUserService oneUserService;
    @Resource
    private NoticeService noticeService;
    @Resource
    private LandService landService;
    @Resource
    private StoreService storeService;
    @Resource
    private OneSuperService oneSuperService;
    @Resource
    private RateDailyRecordService rateDailyRecordService;

    @Resource(name="ehcacheCacheProvider")
    private EhcacheCacheProvider cacheProvider;

    public  FarmController(){
        super("callback");
    }

    /**
     * 刷新首页
     * @param request
     * @return
     */
    @RequestMapping(value = "/refreshHomePage", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult refreshHomePage(HttpServletRequest request,String token,String  userId) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        OneUser oneUser=oneUserService.findByUserId(userId);

        jsonResult.setCode("1");
        Map<String, Object> data= MapUtils.beanToMap(oneUser);

        List<Notice> noticeList=noticeService.findAll();
        List<Land> landList = landService.findAll(oneUser.getFarmId());
        List<RateDailyRecord> rateDailyRecorList = rateDailyRecordService.findByUserId(userId,7,1);
        Store store = storeService.findByUserId(userId);


        if(noticeList.size()>1){
            data.put("notice",noticeList.get(0));
        }else{
            data.put("notice",null);
        }

        Integer totalChicken =0;
        if (landList.size()>0)
        {
            data.put("landList",landList);
            for(int i=0;i<landList.size();i++){
                totalChicken+=landList.get(i).getChicksNum();
            }
        }else {
            data.put("landList", new ArrayList());
        }
        data.put("totalEgg",store.getEggsNum());
        data.put("totalChicken",totalChicken+store.getChicksNum().intValue());

        BigDecimal[] personRateList=new BigDecimal[rateDailyRecorList.size()];
        BigDecimal[] publicRateList =new BigDecimal[rateDailyRecorList.size()];
        if(rateDailyRecorList!=null&&rateDailyRecorList.size()>0){
            for(int i=0;i<rateDailyRecorList.size();i++){
                personRateList[i]=rateDailyRecorList.get(i).getPersonRate();
                publicRateList[i]=rateDailyRecorList.get(i).getPubRate();
            }
        }
        data.put("personRateList", personRateList);
        data.put("publicRateList",publicRateList);
        jsonResult.setData(data);
        return jsonResult;
    }

    /**
     * 查询历史总收成
     * @param request
     * @return
     */
    @RequestMapping(value = "/getTotalRecord", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult getTotalRecord(HttpServletRequest request,String token,String  userId) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        jsonResult.setCode("1");
        Map map = new HashMap();
        map.put("totalEggIncome", 18.03);
        map.put("totalSuperIncome", 200.01);
        map.put("totalClean", 200.01);
        jsonResult.setData(map);
        return jsonResult;
    }

    /**
     * 进入农场
     * @param request
     * @return
     */
    @RequestMapping(value = "/enterChickenFarm", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult enterChickenFarm(HttpServletRequest request,String token,String  userId) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        OneUser oneUser=oneUserService.findByUserId(userId);

        jsonResult.setCode("1");
        Map<String, Object> data=MapUtils.beanToMap(oneUser);

        List<Notice> noticeList=noticeService.findAll();
        List<Land> landList = landService.findAll(oneUser.getFarmId());
        Store store = storeService.findByUserId(userId);
        if(noticeList.size()>1){
            data.put("notice",noticeList.get(0));
        }else{
            data.put("notice",null);
        }
        Integer totalChicken =0;
        if (landList.size()>0)
        {
            data.put("landList",landList);
            for(int i=0;i<landList.size();i++){
                totalChicken+=landList.get(i).getChicksNum();
            }
        }else {
            data.put("landList", new ArrayList());
        }
        data.put("totalEgg",store.getEggsNum());
        data.put("totalChicken",totalChicken+store.getChicksNum().intValue());
        if(!oneUser.getDogLevel().equals("")){
            Map dog = new HashMap();
            dog.put("dogLevel",oneUser.getDogLevel());
            data.put("dog",dog);
        }else {
            data.put("dog",null);
        }
        Map fence = new HashMap();
        fence.put("fenceLevel",oneUser.getFenceLevel());
        data.put("fence",fence);

        OneSuper oneSuper = oneSuperService.find(userId);
        data.put("superIncubator",oneSuper);


        jsonResult.setData(data);
        return jsonResult;
    }

    /**
     * 购买一键清扫
     * @param request
     * @return
     */
    @RequestMapping(value = "/buyQuickclean", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult buyQuickclean(HttpServletRequest request,String token,String  userId) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        Store store = storeService.findByUserId(userId);
        BigDecimal chicksNum = store.getChicksNum();
        if(chicksNum.intValue()<100){
            jsonResult.setCode("-1");
            jsonResult.setMsg("账户小鸡数量不够|Your chicken is not enough");
            return  jsonResult;
        }
        chicksNum= chicksNum.subtract(new BigDecimal(100));
        store.setChicksNum(chicksNum);
        OneUser oneUser = oneUserService.findByUserId(userId);
        oneUser.setKeyCleanDay("30");
        oneUserService.updateOneUserAndStore(oneUser,store);
        jsonResult.setCode("1");
        return  jsonResult;
    }
}
